<template>
  <div>

    <el-form :inline="true" :model="keyword" ref="searchForm" class="demo-form-inline" style="padding-top: 10px;margin-left: 15px">

      <!-- 日期选择 -->
        <el-date-picker
          class="block"
          v-model="value"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      <!-- 下拉框 -->
      <el-form-item style="margin-left: 15px">   <!--    label="全部仓库"    -->
        <el-select v-model="keyword.detailShop"  placeholder="选择店铺" @change="search">
          <el-option v-for="roleType in List3" :label="roleType" :value="roleType" />
        </el-select>
      </el-form-item>

      <!-- 搜索框 -->
      <el-form-item prop="detailId" @click="show = !show">
        <el-input v-model="keyword.detailId" placeholder="搜索内容" v-if="show==true" ></el-input>
      </el-form-item>
      <el-form-item label="SKU" prop="recordsSku">
        <el-input v-model="keyword.recordsSku" placeholder="搜索内容"></el-input>
      </el-form-item>

      <!--查询-->
      <el-form-item>
        <el-button type="primary" @click="search" size="small" icon="el-icon-search">查询</el-button>
        <el-button  plain @click="resetSearchForm('searchForm')" size="small" icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>

      <el-upload
        class="upload-demo"
        drag
        action=""
        :http-request="uploadFile"
        :on-change="handleChange"
        multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>

    </el-form>

    <!-- 表格数据区域 -->
    <el-table
      :data="detailList"
      style="width: 100%;margin-bottom: 20px;"
      row-key="detailId">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="detailPicture"
        label="图片">
      </el-table-column>
      <el-table-column
        prop="recordsSku"
        sortable
        label="品名/SKU">
      </el-table-column>
      <el-table-column
        prop="detailShop"
        label="店铺">
      </el-table-column>
      <el-table-column
        prop="recordsDaohuo"
        label="待到货"></el-table-column>
      <el-table-column
        prop="detailYijian"
        label="已检量"></el-table-column>
      <el-table-column
        prop="listDaijian"
        label="待检量">
      </el-table-column>
      <el-table-column
        prop="detailStatus"
        label="状态">
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 更新对话框  -->
    <el-dialog title="修改数据" :visible.sync="editFormVisible" width="44%">
      <el-form :model="detail" :rules="detailRules" ref="detailRulesForm">
        <el-form-item label="ID" prop="detailId">
          <el-input v-model="detail.detailId" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="SKU" prop="recordsSku">
          <el-input v-model="detail.recordsSku" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="店铺" prop="detailShop">
          <el-input v-model="detail.detailShop" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="detailStatus">
          <el-input v-model="detail.detailStatus" style="width: 300px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editEnter('detailRulesForm')">确 定</el-button>
      </div>
    </el-dialog>

    <!--完整功能分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 5, 7, 10,15,20]"
      :page-size="7"
      layout="total, sizes, prev, pager, next, jumper"
      :total="20">
    </el-pagination>

  </div>
</template>

<script>
import { delDetail, getList, findList, edit } from '@/api/detail'
import { uploadPicture } from '@/api/upload'
export default {
  name: 'index',
  data() {
    return {
      file:null,
      fileSrc:null,
      show: false, // 隐藏输入框
      value: '',  // 日期
      keyword: { // 添加查询关键词对象
        detailShop: '',
        recordsSku: '',
        detailId: '',
      },
      currentPage: 1,
      List3: [],
      detailList: [],
      pageNum: 1,
      pageSize: 7,
      total: 0,
      loading: false,
      editFormVisible: false,
      detail: {
        detailPicture: '',
        recordsSku: '',
        detailShop: '',
        recordsDaohuo: '',
        detailYijian: '',
        listDaijian: '',
        detailStatus: '',
      },
    }
  },
  methods: {
    handleChange(file){
      this.file = file.raw;
    },
    uploadFile(){
      const file = this.file;
      const formData = new FormData();
      formData.append("file", file); // 由后端接口决定,后端参数叫file
      uploadPicture(formData).then((res) => {
        this.$message({
          message: "上传成功",
          type: "success",
        });
        this.fileSrc = res.data
        console.log("fileSrc",this.fileSrc)
      });
    },
    fetchData(){ // 获取数据
      let params = {
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        recordsSku:this.keyword.recordsSku,
        detailId:this.keyword.detailId,
        detailShop:this.keyword.detailShop
      }
      getList(params).then(res => {
        let pageInfo = res.data;
        this.loading = false;
        this.detailList = pageInfo.list;
        this.total = pageInfo.total
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData(); // 重新获取数据
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.fetchData(); // 重新获取数据
    },
    // 搜索
    search() {
      this.fetchData()
    },
    // 重置
    resetSearchForm(formName) {
      this.$refs[formName].resetFields();
      this.fetchData()
    },
    // 修改框
    handleUpdate(row) {
      this.editFormVisible = true
      this.detail = row
    },
    // 确认
    editEnter(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          edit(this.detail).then(res => {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '修改成功!',
            })
          })
        } else {
          return false
        }
        this.editFormVisible = false
      })
    },
    // ID删除
    handleDelete(row){
      this.$confirm('是否确认删除品名/SKU为'+row.recordsSku+'的数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          id:row.detailId
        }
        delDetail(params).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetchData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    find3() {
      findList().then(res => {
        this.List3 = res.data
        this.fetchData()
      })
    }
  },
  created() {
    this.fetchData()
    this.find3()
  },
}
</script>

<style scoped>

</style>
